feed.json
放在 S3 bucket 中package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// Feed represents feed.json structure
type Feed struct {
Podcast struct {
LastBuildDate string `json:"lastBuildDate"`
Title string `json:"title"`
Description string `json:"description"`
Image struct {
URL string `json:"url"`
Title string `json:"title"`
Link string `json:"link"`
} `json:"image"`
Link string `json:"link"`
Author string `json:"author"`
Copyright string `json:"copyright"`
Email string `json:"email"`
Language string `json:"language"`
Type string `json:"type"`
CategoryMain string `json:"category_main"`
CategorySub string `json:"category_sub"`
Explicit string `json:"explicit"`
Episodes []struct {
Title string `json:"title"`
Description string `json:"description"`
Link string `json:"link"`
Audio string `json:"audio"`
Creator string `json:"creator"`
Explicit string `json:"explicit"`
Duration string `json:"duration"`
Image string `json:"image"`
Episode int `json:"episode"`
Type string `json:"type"`
} `json:"episodes"`
} `json:"podcast"`
}
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bootstrap -tags lambda.norpc main.go
// zip myFunction.zip bootstrap
func main() {
lambda.Start(handler)
}
func handler(ctx context.Context, s3Event events.S3Event) error {
sess := session.Must(session.NewSession())
svc := s3.New(sess)
object := s3Event.Records[0].S3.Object
bucket := "intoxicating"
key := object.Key
// Download object from S3
resp, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
fmt.Println("Error while downloading the object", err)
return err
}
defer resp.Body.Close()
// Decode JSON payload into struct
var feed Feed
json.NewDecoder(resp.Body).Decode(&feed)
// Generate RSS feed based on feed.json
fmt.Println(feed)
rssFeed := generateRSS(feed)
fmt.Println("RSS feed input:")
// Upload RSS feed to S3 root path as feed.rss
var body bytes.Buffer
body.Write(rssFeed)
fmt.Println("RSS feed output:")
fmt.Println(string(rssFeed))
uploadResp, err := svc.PutObject(&s3.PutObjectInput{
Body: bytes.NewReader(body.Bytes()),
Bucket: aws.String(bucket),
Key: aws.String("feed.rss"),
ContentType: aws.String("application/xml"),
})
if err != nil {
fmt.Println("Error while uploading the object", err)
return err
}
fmt.Printf("Feed uploaded to S3 with status %v\n", uploadResp)
return nil
}
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bootstrap -tags lambda.norpc main.go
zip myFunction.zip bootstrap